package com.jinzhi.jzweb.controller;


import java.util.*;

import com.jinzhi.common.service.DictService;
import com.jinzhi.common.utils.FileUtil;
import com.jinzhi.jzapi.common.controller.SchoolApittController;
import com.jinzhi.jzapi.common.pojo.SchoolCerUtilsVO;
import com.jinzhi.jzweb.domain.SchoolCertificatesVO;
import com.jinzhi.jzweb.pojo.TrainingCerVo;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.jzweb.utils.ValuesUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.SchoolCertificatesDO;
import com.jinzhi.jzweb.service.SchoolCertificatesService;
import com.jinzhi.common.utils.Result;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * <pre>
 * 学校证书
 * </pre>
 * <small> 2022-11-03 09:55:21 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/schoolCertificates")
public class SchoolCertificatesController extends BaseController {

	@Autowired
	private DictService dictService;

	@Autowired
	private SchoolCertificatesService schoolCertificatesService;

	@Autowired
	private SchoolApittController schoolApittController;
	
	@GetMapping()
	@RequiresPermissions("jzweb:schoolCertificates:schoolCertificates")
	String SchoolCertificates(){
	    return "jzweb/schoolCertificates/schoolCertificates";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:schoolCertificates:schoolCertificates")
	public Result<Page<SchoolCertificatesDO>> list(SchoolCertificatesDO schoolCertificatesDTO){
		if(schoolCertificatesDTO.getOrgName()==""){
			schoolCertificatesDTO.setOrgName(null);
		}
		// 查询列表数据
		Page<SchoolCertificatesDO> page = schoolCertificatesService.selectPage(getPage(SchoolCertificatesDO.class),
					schoolCertificatesService.convertToEntityWrapper().like("org_name",schoolCertificatesDTO.getOrgName()).orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				SchoolCertificatesDO obj = (SchoolCertificatesDO) page.getRecords().get(i);
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	


	
	@Log("删除学校证书")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:schoolCertificates:remove")
	public Result<String>  remove( Long id){
		schoolCertificatesService.deleteById(id);
        return Result.ok();
	}
	


	/**
	 * 下载导入模板
	 *
	 * @param response
	 */
	@RequestMapping("/downloadImportTemplate")
	@RequiresPermissions("jzweb:schoolCertificates:import")
	@ResponseBody
	public void downloadImportTemplate(HttpServletResponse response) {
		List<SchoolCertificatesVO> list = new LinkedList<>();
		//导出操作
		EasyPoiUtil.exportExcel(list, "学校证书导入模板", "学校证书导入模板", SchoolCertificatesVO.class, "学校证书导入模板", response);
	}




	/**
	 * 培训证书信息导入
	 *
	 */
	@Log("培训证书信息导入")
	@PostMapping("/import")
	@ResponseBody
	@Transactional
	@RequiresPermissions("jzweb:schoolCertificates:import")
	public Result<?> importExcel(@RequestParam("file") MultipartFile file,String certificatesType, String type,String getTime){
		//第一步--判断文件格式
		if (!"application/vnd.ms-excel".equals(file.getContentType())) {
			return Result.fail("文件格式错误，请选择.xls文件");
		}
		//第二步--解析文件获得数据
		List<SchoolCertificatesVO> list = EasyPoiUtil.importExcel(file, 1, 1, SchoolCertificatesVO.class);
		//第三步--参数校验
		parameterCheck(list,certificatesType,type,getTime);

		//第四步--查询模板
		String traSynthesis = dictService.selDicBy(selThetemplate(type,certificatesType));
		if (StringUtils.isNotBlank(traSynthesis)) {
			traSynthesis = ValuesUtil.localUrl + traSynthesis;
		}
		System.out.println("模板地址为++++++++++++++++++++++++++++++++++++++++++++++++++++"+traSynthesis);

		//第五步--获取电子公章
		String electronic = getElectronic();
		System.out.println("电子公章地址为++++++++++++++++++++++++++++++++++++++++++++++++++++"+electronic);

		//定义数据库修改集合以及证书生成集合
		List<SchoolCerUtilsVO> schoolCerUtilsVOS =new ArrayList<>();
		List<SchoolCertificatesDO> schoolCertificatesDOList =new ArrayList<>();

		for (int i=0;i<list.size();i++){
			SchoolCerUtilsVO schoolCerUtilsVO =new SchoolCerUtilsVO();
			schoolCerUtilsVO.setBackgroundPath(traSynthesis); //证书模板地址
			schoolCerUtilsVO.setOrgName(list.get(i).getOrgName()); //学校名称
			schoolCerUtilsVO.setCode(list.get(i).getCode()); //站点编码
			schoolCerUtilsVO.setCerYear(getTime.substring(0, 4)); //年份
			schoolCerUtilsVO.setCerMath(getTime.substring(5, 7)); //月份
			schoolCerUtilsVO.setCertificatesType(Integer.valueOf(certificatesType));
			schoolCerUtilsVO.setElectronicSeal(electronic);
			SchoolCertificatesDO schoolCertificatesDO = getSchoolCertificatesDO(schoolCerUtilsVO, getTime, type,list.get(i).getProvince());
			schoolCerUtilsVOS.add(schoolCerUtilsVO);
			schoolCertificatesDOList.add(schoolCertificatesDO);
		}


		boolean r = schoolCertificatesService.insertBatch(schoolCertificatesDOList);
		if (r) {
			if ("1".equals(type)) {
				schoolApittController.yezhzs(schoolCerUtilsVOS, schoolCertificatesDOList);
			}
			if ("2".equals(type)) {
				schoolApittController.chhfzs(schoolCerUtilsVOS, schoolCertificatesDOList);
			}
		}
		return Result.ok("成功导入" + list.size() + "条数据");
	}

	//参数校验
	private Result<?> parameterCheck(List<SchoolCertificatesVO> list, String certificatesType, String type,String getTime){
		if (list.size() == 0) {
			return Result.fail("Excel数据为空，请完善");
		}
		if (StringUtils.isBlank(certificatesType)) {
			return Result.fail("请选择证书类型！");
		}
		if (StringUtils.isBlank(type)) {
			return Result.fail("请选择幼儿照护或产后恢复！");
		}
		if (StringUtils.isBlank(getTime)) {
			return Result.fail("请选择证书生成日期！");
		}
		return null;
	}

	//模板查询
	private Map selThetemplate(String type,String certificatesType){
		Map map = new HashMap();
		if ("1".equals(type)){ //幼儿照护
			if (certificatesType.equals("1")) {
				map.put("name", "幼儿照护考核站点-牌匾");
				map.put("type", "yezh_khzd_pb");
			}
		}
		if ("2".equals(type)){ //产后恢复
			if (certificatesType.equals("1")) {
				map.put("name", "产后恢复考核站点-牌匾");
				map.put("type", "chhf_khzd_pb");
			}
		}
		return map;
	}

	//获取电子公章
	private String getElectronic(){
		String electronicSeal = null;
		//获取电子公章
		Map map = new HashMap();
		map.put("name", "金职电子公章");
		map.put("type", "electronic_seal");
		electronicSeal = dictService.selDicBy(map);
		if (StringUtils.isNotBlank(electronicSeal)) {
			electronicSeal = ValuesUtil.localUrl + electronicSeal;
		}
		return electronicSeal;
	}

	//证书编号生成规则
	private String getNumbers(String type,int certificatesType,String code,String province){
		//证书编号
		String cerNumEnd = "JZ";

		Random random=new Random();
		int sj=random.nextInt(9999-1000+1)+1000;

		//学校牌匾生成规则，JZ + 幼儿照护 + 考核站点 + 站点编码 + 四位随机数
		String number = cerNumEnd + type + String.valueOf(certificatesType) + code + String.valueOf(sj) + selProvinceCode(province);

		return number;
	}

	//组装SchoolCertificatesDO表数据
	private SchoolCertificatesDO getSchoolCertificatesDO(SchoolCerUtilsVO schoolCerUtilsVO,String getTime,String type,String province){
		SchoolCertificatesDO schoolCertificatesDO =new SchoolCertificatesDO();
		schoolCertificatesDO.setOrgName(schoolCerUtilsVO.getOrgName()); //学校名称
		schoolCertificatesDO.setNumbers(getNumbers(type,schoolCerUtilsVO.getCertificatesType(),schoolCerUtilsVO.getCode(),province));
		schoolCertificatesDO.setProvince(province);
		schoolCertificatesDO.setGetTime(getTime); //获证时间
		schoolCertificatesDO.setCertificatesType(schoolCerUtilsVO.getCertificatesType()); //证书类型
		schoolCertificatesDO.setQueries(0);//查询次数
		schoolCertificatesDO.setState(0);//状态
		schoolCertificatesDO.setCode(schoolCerUtilsVO.getCode());//站点编码
		return schoolCertificatesDO;
	}



	public String selProvinceCode(String province) {
		if (province.equals("北京市")) {
			return "11";
		} else if (province.equals("天津市")) {
			return "12";
		} else if (province.equals("河北省")) {
			return "13";
		} else if (province.equals("山西省")) {
			return "14";
		} else if (province.equals("内蒙古")) {
			return "15";
		} else if (province.equals("辽宁省")) {
			return "21";
		} else if (province.equals("吉林省")) {
			return "22";
		} else if (province.equals("黑龙江")) {
			return "23";
		} else if (province.equals("上海市")) {
			return "31";
		} else if (province.equals("江苏省")) {
			return "32";
		} else if (province.equals("浙江省")) {
			return "33";
		} else if (province.equals("安徽省")) {
			return "34";
		} else if (province.equals("福建省")) {
			return "35";
		} else if (province.equals("江西省")) {
			return "36";
		} else if (province.equals("山东省")) {
			return "37";
		} else if (province.equals("河南省")) {
			return "41";
		} else if (province.equals("湖北省")) {
			return "42";
		} else if (province.equals("湖南省")) {
			return "43";
		} else if (province.equals("广东省")) {
			return "44";
		} else if (province.equals("广西")) {
			return "45";
		} else if (province.equals("海南省")) {
			return "46";
		} else if (province.equals("重庆市")) {
			return "50";
		} else if (province.equals("四川省")) {
			return "51";
		} else if (province.equals("贵州省")) {
			return "52";
		} else if (province.equals("云南省")) {
			return "53";
		} else if (province.equals("西藏")) {
			return "54";
		} else if (province.equals("陕西省")) {
			return "61";
		} else if (province.equals("甘肃省")) {
			return "62";
		} else if (province.equals("青海省")) {
			return "63";
		} else if (province.equals("宁夏")) {
			return "64";
		} else if (province.equals("新疆")) {
			return "65";
		} else if (province.equals("新疆兵团")) {
			return "66";
		} else if (province.equals("台湾")) {
			return "71";
		} else if (province.equals("香港")) {
			return "81";
		} else if (province.equals("澳门")) {
			return "82";
		} else if (province.equals("海外")) {
			return "91";
		} else {
			return "00"; //未知省份
		}
	}
}
